
//渠道
ascription2()
function ascription2() {
    $.ajax({
        type: "post",
        url: root.dockingplatform.allChannelConfig,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {

        },
        success: function (data) {
            console.log(data)
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#ascription_lsit").html(interText(dataInter));
            }

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}


/*获取收货地址*/
discountdata(123)
function discountdata (val) {
    if(val==123){
        //初始化 省
        var user_data = {
            level: '101',
            // memberId:'10258',
        }
        var succCallBack = function (data, status, response) {
            console.log(data)
            if(data.errorCode=="1"){
                var tab_data=data.list;
                if(tab_data==''){
                    cer+='<option value=""></option>'
                    $("#getArea").html(cer);
                }else{
                    var cer="<option value=''>请选择收货区域</option>"
                    for(var i=0; i<tab_data.length; i++){
                        cer+='<option value="'+tab_data[i].address_id+'">'+tab_data[i].name+'</option>'
                    }
                    $("#getArea").html(cer);
                }
            }
        };
        web.doAjax(root.orderInter.order_return_address,'post',user_data, succCallBack);
    }else if(val==1){
        //市
        var special=$('#getArea').val()
        if(special==""){
            var cer="<option value=''>请选择收货位置</option>"
            $("#getPlace").html(cer);
        }else {
            var user_data = {
                level: '102',
                // memberId:'10258',
                addressIdStr:special
            }
            var succCallBack = function (data, status, response) {
                if(data.errorCode=="1"){
                    var tab_data=data.list;
                    if(tab_data==''){
                        cer+='<option value=""></option>'
                        $("#getPlace").html(cer);
                    }else{
                        var cer="<option value=''>请选择收货位置</option>"
                        for(var i=0; i<tab_data.length; i++){
                            cer+='<option value="'+tab_data[i].address_id+'">'+tab_data[i].name+'</option>'
                        }
                        $("#getPlace").html(cer);
                    }
                }
            };
            web.doAjax(root.orderInter.order_return_address,'post',user_data, succCallBack);
        }
    }
}
/*表格渲染*/
var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];

function orderList(data) {
    console.log(data)
    dataSource.splice(0, dataSource.length);//清空数组
    if(data.data!=null){

    $.each(data.data.result, function (i, field) {
        var rowData = new Array();
        rowData.push(field.maintainId);
        rowData.push(field.channelOrderId);
        rowData.push(field.orderId);
        rowData.push(field.maintainReason);
        rowData.push(field.maintainType);
        rowData.push(field.createdAt);
        rowData.push(field.auditTime);
        rowData.push(field.listAddressReceiver[0]);
        rowData.push(field.maintainStatus);
        dataSource.push(rowData);
    });

    }
    initDataTable(dataSource);

    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "order": [[ 5, "desc" ]],
            "autoWidth": false,
            "ordering": false,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },


                {
                    "render": function (data, type, row) {
                        var maintainType = "";
                        switch (row[3]) {
                            case 10:
                                maintainType = "电机/主机不转、功率不够";
                                break;
                            case 20:
                                maintainType = "机器劲小、钻不动孔";
                                break;
                            case 30:
                                maintainType = "调速功能有问题";
                                break;
                            case 40:
                                maintainType = "开关坏";
                                break;
                            case 50:
                                maintainType = "电源问题";
                                break;
                            case 60:
                                maintainType = "机器不发动";
                                break;
                            case 70:
                                maintainType = "外机破损";
                                break;
                            case 80:
                                maintainType = "商品质量问题";
                                break;
                        }
                        var val = '<span>' + maintainType + '</span>';
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row) {
                        var repairType = "";
                        switch (row[4]) {
                            case 10:
                                repairType = "客户自行送维修点";
                                break;
                            case 20:
                                repairType = "厂家上门维修";
                                break;
                            case 30:
                                repairType = "鑫方盛售后上门维修";
                                break;
                        }
                        var val = '<span>' + repairType + '</span>';
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var d = new Date(row[5]);
                        var val=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var d = new Date(row[6]);
                        var val=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                        var hhh
                        if (row[7]==undefined || row[7]==null){
                            hhh=""
                        }else{
                            hhh=row[7].receiverName
                        }
                        var val = '<div>' + hhh + '</div>';
                        return val;
                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row, meta) {
                        var status = "";
                        switch (row[8]) {
                            case 10:
                                status = "待审核"
                                break;
                            // case 20:
                            //     status = "已受理"
                            //     break;
                            case 30:
                                status = "维修处理"
                                break;
                            case 50:
                                status = "已完成"
                                break;
                            case 60:
                                status = "已关闭"
                                break;
                        }
                        var val = '<span>' + status + '</span>';
                        return val;
                    },
                    "targets": 8
                },
                {
                    "render": function (data, type, row) {
                        var val = '<span class=""><a class="list powerButton 623" onclick="orderInfo(\'申请单详情\',\'afterSaleService-maintain.html\',\''+row[0]+'\',\''+row[2]+'\')">查看</a></span>';
                        return val;
                    },
                    "targets": 9
                }
            ]
        });
        //权限控制函数
        power()
    };
}
function getList(pagenum){
    
    // if ($('#ascription_lsit option:selected') .val()==""){
    //
    //     layer.msg("请选择渠道商");
    //     return;
    // }
    if($("#ascription_lsit").val() == null){
        a = '';
    }else{
        a = $("#ascription_lsit").val()
    }
    $.ajax({
        contentType: "application/json",
        type: "post",
        url: root.dockingplatform.queryPreMaintainList,
        cache: false,  //禁用缓存
//        dataType: "json",
        data: JSON.stringify({
            "pageNum":pagenum,
            "pageSize":50,
            "customerCode":a,//渠道商id
            "maintainId":$("#applyCode").val(),// 申请编号
            "channelOrderId":$("#dorderCode").val(),//对方单号
            "orderId":$("#orderCode").val(),//我方单号
            "maintainType":$("#mainType").val(),//维修类型
            "createdAtBegin":$("#applyTimeMin").val(),// 申请开始时间
            "createdAtEnd":$("#applyTimeMax").val(),// 申请结束时间
            "parentWarehouseId":$("#shippingWarehouse option:selected").val(),// 仓库id
            "maintainStatus":$("#dealState").val(),//维修单状态
            "maintainReason":$("#maintainReason").val(),//申请原因
            "auditTimeBegin":$("#checkTimeMin").val(),// 审核起始时间
            "auditTimeEnd":$("#checkTimeMax").val(),// 审核终止时间
            "salesManagerName":$("#saleManager").val(),// 销售经理名称
            "auditPersonName":$("#checker").val(),// 审核人
        }),
        success: function (result) {
            sessionStorage.setItem("maintainstatus",'')
            orderList(result);
            $('.M-box3').pagination({
                pageCount: result.data.totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        contentType: "application/json",
                        type: "POST",
                        url: root.dockingplatform.queryPreMaintainList,
                        cache: false,  //禁用缓存
                        dataType: "json",
                        data: JSON.stringify({
                            "customerCode":a,//渠道商id
                            "maintainId":$("#applyCode").val(),// 申请编号
                            "channelOrderId":$("#dorderCode").val(),//对方单号
                            "orderId":$("#orderCode").val(),//我方单号
                            "maintainType":$("#mainType").val(),//维修类型
                            "createdAtBegin":$("#applyTimeMin").val(),// 申请开始时间
                            "createdAtEnd":$("#applyTimeMax").val(),// 申请结束时间
                            "parentWarehouseId":$("#shippingWarehouse option:selected").val(),// 仓库id
                            "maintainStatus":$("#dealState").val(),//维修单状态
                            "maintainReason":$("#maintainReason").val(),//申请原因
                            "auditTimeBegin":$("#checkTimeMin").val(),// 审核起始时间
                            "auditTimeEnd":$("#checkTimeMax").val(),// 审核终止时间
                            "salesManagerName":$("#saleManager").val(),// 销售经理名称
                            "auditPersonName":$("#checker").val(),// 审核人
                            "pageNum":api.getCurrent(),
                            "pageSize":50
                        }),
                        success: function (result) {

                            orderList(result);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log( "响应吗 = "+ XMLHttpRequest.status);
            console.log( "readyState = "+ XMLHttpRequest.readyState);
        }
    });
}

/*跳转订单详情*/
function orderInfo(title,url,maintainId,orderId){
    sessionStorage.setItem("maintainOrderId",orderId);
    sessionStorage.setItem("maintainId",maintainId);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
//弹框跳转
var maintainstatus= sessionStorage.getItem("maintainstatus")

if(maintainstatus==10){
    $('#dealState').val(maintainstatus)
    getList(1)
}
